interface IPageProps<T> {
  records: T[]; // 数据列表 
  current: number; // 当前页
  pages: number; // 总页数
  size: number; // 每页显示条数，默认 10 
  total: number; // 总数
}

const firstPage = 1; // 第一页是从1开始

export default class PageModel<T> implements IPageProps<T>{
  records: T[] = [];
  current = firstPage;
  pages = 0;
  size = 10;
  total = 0;

  constructor(props?: IPageProps<T>) {
    if (props) {
      const { records, pages, size, total, current } = props;
      this.records = records;
      this.pages = pages;
      this.size = size;
      this.total = total;
      this.current = current;
    }
  }

  updateRecords(newData: PageModel<T>) {
    if (newData.current == 1) {
      // 第一页的话，数据从头开始
      this.records = newData.records;
    } else {
      // 非第一页，一直追加
      this.records = this.records.concat(newData.records);
    }
    this.current = newData.current;
    this.pages = newData.pages;
    this.size = newData.size;
    this.total = newData.total;
  }

  get haveMore(): boolean {
    return this.current < this.pages;
  }
}

